$(function() {
	$('.muser-delete').on('click', function() {
		$('#del-confirm').modal({
			relatedTarget : this,
			onConfirm : function(options) {
				var id = $(this.relatedTarget).attr("data-id");
				window.location="deleteMUser?id=" + id;
			},
			onCancel : function() {

			}
		});
	});
	$("#sub-btn").click(function() {
		var username = $("#username");
		var truename = $("#truename");
		var role = $("#role-select");
		if(check_username(username) && check_truename(truename) && check_role(role)){
			$("#mng-user-form").submit();
		}
	});
	function check_username(usernameObj) {
		var username = usernameObj.val();
		var muserId = $("#mng-user-id").val();
		var result = true;
		if(username == ""){
			alert("请填写用户名");
			result = false;
		} else if(muserId == ""){
			$.ajax({
				url : 'checkUsername',
				type : 'post',
				async : false,
				data : {"username":username},
				dataType : 'json',
				success : function(data) {
					var status = data.status;
					if(status == 500){
						result = false;
						alert("用户名重复!");
					}
				}
			});
		}
		return result;
	}
	function check_truename(truenameObj){
		var truename = truenameObj.val();
		if(truename == ""){
			alert("请填写真实姓名");
			return false;
		}
		return true;
	}
	function check_role(roleObj){
		var roleId = roleObj.val();
		if(roleId == ""){
			alert("请选择用户角色");
			return false;
		}
		return true;
	}
});